package com.controller.sys.user;

import com.common.ErrorCodeEnum;
import com.common.emums.UserType;
import com.common.utils.EntityConversionUtil;
import com.common.utils.StringUtils;
import com.common.anntation.Log;
import com.common.emums.BusinessType;

import com.common.excel.ExcelUtil;
import com.common.exception.BusinessException;
import com.common.utils.DateUtils;
import com.core.springSecurity.service.PermissionService;
import com.model.domain.sys.GetUserListDO;
import com.model.dto.sys.userController.GetUserListDTO;
import com.model.pojo.sys.SysDept;
import com.model.pojo.sys.SysUser;
import com.model.pojo.excel.UserExcel;
import com.service.sys.SysDeptService;
import com.service.sys.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import javax.annotation.Resource;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author: zql
 * @Description: 导出用户模块
 * @Data: 15:20 2021/7/9
 */

@Api(tags = {"用户"})
@Slf4j
@RestController
@RequestMapping("/v1")
public class ExportUserController {

    @Resource
    SysUserService sysUserService;

    @Resource
    SysDeptService sysDeptService;
    /**
     * @Author: zql
     * @Description: 导出用户表
     * @param getUserListDTO 导出参数
     * @Data: 8:50 2021/6/3
     */
    @ApiOperation(value = "导出用户表")
    @Log(title = "导出用户表" ,businessType = BusinessType.EXPORT,isSaveResponseData = false)
    @PostMapping(value = "/exportUserInfo")
    @PreAuthorize("@ss.hasAnyPermi('" + PermissionService.SYSTEM_OPERATE + "')")
    public void exportUserInfo(@RequestBody @Valid GetUserListDTO getUserListDTO, HttpServletResponse response) throws BusinessException {
        GetUserListDO getUserListDO= EntityConversionUtil.copy(getUserListDTO,GetUserListDO.class);
        getUserListDO.setUserType(UserType.USER.getCode());
        List<SysUser> sysUserList = sysUserService.getUser(getUserListDO);
        List<UserExcel> sysUserExcelList = new ArrayList<>();
        for (SysUser sysUser : sysUserList) {
            UserExcel sysUserExcel = new UserExcel();
            BeanUtils.copyProperties(sysUser,sysUserExcel);
            SysDept sysDept= sysDeptService.get(sysUser.getSysDeptId(),null);
            if(StringUtils.isNull(sysDept)){
                log.error("用户绑定了不存在部门,导出用户表失败,用户id:{},不存在部门id:{}",sysUser.getSysUserId(),sysUser.getSysDeptId());
                throw new BusinessException(ErrorCodeEnum.DATA_ABNORMAL.setParam("用户绑定了不存在部门,用户id:"+sysUser.getSysUserId()+" 不存在部门id:"+sysUser.getSysDeptId()));
            }
            sysUserExcel.setDeptName(sysDept.getDeptName());
            sysUserExcel.setPassword(null);
            sysUserExcelList.add(sysUserExcel);
        }
        String fileName = "用户信息表" + DateUtils.parseDateToStr(DateUtils.YYYYMMDD_HHMM,new Date());
        try {
            ExcelUtil.writeExcel(response, sysUserExcelList, fileName, "用户信息", new UserExcel());
        } catch (IOException e) {
            log.error("导出失败，文件名:{}",fileName);
            e.printStackTrace();
        }
    }

    /**
     * 导出用户表模板
     *
     * @return 结果
     */
    @ApiOperation(value = "导出用户表模板")
    @Log(title = "导出用户表模板" ,businessType = BusinessType.EXPORT,isSaveResponseData = false)
    @PostMapping(value = "/exportUserInfoDemo")
    @PreAuthorize("@ss.hasAnyPermi('" + PermissionService.SYSTEM_OPERATE + "')")
    public void exportUserInfo(HttpServletResponse response) throws BusinessException {
        List<UserExcel> sysUserExcelList = new ArrayList<>();
        String fileName = "用户信息表" + DateUtils.parseDateToStr(DateUtils.YYYYMMDD_HHMM, new Date());
        try {
            ExcelUtil.writeExcel(response, sysUserExcelList, fileName, "用户信息", new UserExcel());
        } catch (IOException e) {
            log.error("导出失败，文件名:{}", "fileName");
        }
    }
}
